/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.api;

import axil.api.error.AxilException;
import axil.api.extend.Environment;
import axil.api.extend.Parameters;


/**
 * An interface representing a native Axil object. Axil does not directly use
 * Java objects, but instead uses its own object system, wrapping or replacing
 * the Java objects with Axil equivalents. This is done to provide a language
 * that is more friendly to the novice or casual script writer. For example,
 * Axil has a proper numerical tower which is immune to integer overflow.
 * <p/>
 * This interface is not intended to be implemented by host applications. This
 * interface provides a mechanism for encapsulating the implementation. Use the
 * Axil.instance() object to access instances of this object.
 */
public interface AxilObject extends Cloneable, Comparable<AxilObject> {
	/**
	 * Get the type metadata for this object. The type metadata contains
	 * information about the type itself, as an object. When host application
	 * types are defined, Axil will find the type metadata through one of two
	 * mechanisms:
	 * <pre>
	 *     <ul>
	 *         <li>
	 *             An attempt is made to create an anonymous instance of the
	 *             type via a public, parameter-less constructor.
	 *     	   </li>
	 *     	   <li>
	 *     	       If Such a constructor does not exist, then the class is
	 *     	       scanned for a public, static method named instance() that
	 *     	       returns the type metadata.
	 *     	   </li>
	 *     </ul>
	 * </pre>
	 * If neither of these mechanisms is available, then an exception is thrown.
	 *
	 * @return
	 * 	Returns the type metadata, which is never null.
	 */
	public AxilType type();


	/**
	 * Tell if this object, represents a true or false value. In general, true
	 * is returned for any object that is not equivalent to nil, is a number
	 * greater than zero, or is the boolean true value.
	 */
	public boolean bool();


	/**
	 * Return the host application (or built-in Java) object that most closely
	 * matches this Axil object.
	 *
	 * @return
	 * 	Return returned value is never null unless this Axil object represents
	 * 	the special 'nil' object.
	 *
	 * 	@see axil.api.Axil#NIL
	 */
	public Object intrinsic();


	/**
	 * Coerce this object into an instance of the given type. If this object
	 * cannot be meaningfully represented as an instance of that type, then an
	 * exception is thrown.
	 *
	 * @param type
	 * 	The type to be coerced to. The object given cannot be null.
	 *
	 * @return
	 * 	Returns an instance of the given type. If this object is nil, then nil
	 * 	is returned.
	 */
	public AxilObject coerce(AxilType type) throws AxilException;


	/**
	 * Tell if this object is equal to the given object. The object given
	 * is never null. The object given may be of any type of value object.  If
	 * the given object is not a suitable type for comparison, a
	 * ClassCastException may be thrown.
	 */
	public abstract boolean equalTo(AxilObject object);


	/**
	 * Tell if this object is roughly equivalent to the given object. That is,
	 * would a non-technical person consider the objects equivalent? For example,
	 * a list(1,2,3) is equivalent to the list(3,2,1), but those lists are not
	 * exactly equal. The object given is never null. The object given may be
	 * of any type of value object. If the given object is not a suitable type
	 * for comparison, a ClassCastException may be thrown.
	 */
	public abstract boolean equivalentTo(AxilObject object);


	/**
	 * Tell if this object is exactly equal to the given object. Exact equality
	 * takes type, order and all aspects of the object into consideration. An
	 * object can only be exactly equal to another if it is the exact same type.
	 * The object given is never null. The object given may be of any type of
	 * value object. If the given object is not a suitable type for comparison,
	 * a ClassCastException may be thrown.
	 */
	public abstract boolean exactly(AxilObject object);


	/**
	 * Compares this object with the specified object for order. If the given
	 * object is not a suitable type for comparison, a ClassCastException may
	 * be thrown.
	 *
	 * @param object
	 * 	The object to compare against. The given object cannot be null but may
	 * 	be any Axil object.
	 *
	 * @return
	 * 	Returns a negative integer, zero, or a positive integer as this object
	 * 	is less than, equal to, or greater than the specified object.
	 */
	public int comparedTo(AxilObject object);


	/**
	 * Evaluate this object in the given context and environment.
	 *
	 * @param env
	 * 	The environment in which the function is being invoked. The environment
	 * 	is never null.
	 *
	 * @param params
	 * 	The parameters to the function, which can be any Axil object. The values
	 * 	given are never null but may be the nil object (Axil.NIL).
	 */
	public AxilObject eval(Environment env, Parameters params);
}
